package com.sun.modules.register.controller;
/**
 * @author 赵东朝
 * @date  2018-8-10
 */

import com.sun.common.utils.DateUtils;
import com.sun.common.utils.FileUtils;
import com.sun.common.utils.R;
import com.sun.common.utils.StringUtil;
import com.sun.modules.register.entity.ClubMemberEntity;
import com.sun.modules.register.service.RegisterService;
import com.sun.modules.sys.controller.AbstractController;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/register/")
public class RegisterController extends AbstractController {
    @Value("${spring.http.multipart.location}")
    private String uploadPath;
    @Autowired
    private RegisterService registerService ;
    @PostMapping("add")
    public R forRegister(MultipartHttpServletRequest multipartHttpServletRequest){
        R result ;
        ClubMemberEntity clubMemberEntity = parseRequest(multipartHttpServletRequest);
        List<MultipartFile> fileList = multipartHttpServletRequest.getFiles("file");
        for (MultipartFile file : fileList){
            if (!file.isEmpty()){
                FileUtils.writeFile(file,uploadPath);
                if (StringUtils.isEmpty(clubMemberEntity.getPicture1())){
                    clubMemberEntity.setPicture1(file.getOriginalFilename());
                    continue;
                }
                if (StringUtils.isEmpty(clubMemberEntity.getPicture2())){
                    clubMemberEntity.setPicture2(file.getOriginalFilename());
                    continue;
                }
                if (StringUtils.isEmpty(clubMemberEntity.getPicture3())){
                    clubMemberEntity.setPicture3(file.getOriginalFilename());
                }
            }
        }
        if (registerService.queryList(clubMemberEntity).size()==0){
            registerService.save(clubMemberEntity);
            result = R.ok();
        }else {
            result = R.error(1,"亲爱的"+clubMemberEntity.getUsername()+",您的信息已经被占用！");
        }
      return result ;
    }

    private ClubMemberEntity parseRequest(HttpServletRequest request) {
        ClubMemberEntity clubMember = new ClubMemberEntity();
        String username = request.getParameter("username");
        String birthday = request.getParameter("birthday");
        String sex = request.getParameter("sex");
        double height = Double.parseDouble(request.getParameter("height"));
        String job = request.getParameter("job");
        String interest = request.getParameter("interest");
        String telphone= request.getParameter("telphone");
        String email= request.getParameter("email");
        String qq= request.getParameter("qq");
        String weixin= request.getParameter("weixin");
        String sina= request.getParameter("sina");
        String txwb= request.getParameter("txwb");
        String plan= request.getParameter("plan");
        String picture1= request.getParameter("picture1");
        String picture2= request.getParameter("picture2");
        String picture3= request.getParameter("picture3");

        Date join_time= Calendar.getInstance().getTime();
        Date update_time= Calendar.getInstance().getTime();

        //TODO 需要数据校验，目前第一版不做
        clubMember.setId(StringUtil.createId());
        clubMember.setUsername(username);
        clubMember.setBirthday(birthday);
        clubMember.setSex(sex);
        clubMember.setHeight(height);
        clubMember.setJob(job);
        clubMember.setInterest(interest);
        clubMember.setTelphone(telphone);
        clubMember.setEmail(email);
        clubMember.setQq(qq);
        clubMember.setWeixin(weixin);
        clubMember.setSina(sina);
        clubMember.setTxwb(txwb);
        clubMember.setPlan(plan);
        clubMember.setPicture1(picture1);
        clubMember.setPicture2(picture2);
        clubMember.setPicture3(picture3);

        clubMember.setJoin_time(join_time);
        clubMember.setUpdate_time(update_time);
        return clubMember;
    }
}
